package com.eascs.admin.o2o.statistics.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.text.Format;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.loan.controller.LoanapplyController;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.app.dao.PageResult;
import com.eascs.common.doc.excel.DefaultRowWriteHandler;
import com.eascs.common.doc.excel.ExcelApiHelper;
import com.eascs.common.exception.RuntimeIOException;
import com.eascs.common.util.IoUtil;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.ViewContext;
import com.eascs.tag.MenuTag;

@Controller
@RequestMapping("/statis")
public class O2oStatisticsController  extends BaseMasterViewController {
	@Override
	public void preHandle() {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "report");
	}
	@Autowired
	private O2oLoanapplyService loanService;
	/**
	 * @Description:
	 *               <p>
	 *               loan-11722 增加业务汇总统计功能
	 *               </p>
	 * @author CXW
	 * @date 2017年6月23日 下午1:57:29
	 * @version V1.0
	 */
	@RequestMapping("/find_statis")
	public String findLoanApplyStatis(){
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "statistic");
		String o2ourl = "/statis/find_statis";
		String reporto2ourl = "/statis/download_statis";
		String titles[]={"省区","新建笔数","新建金额","准入中笔数","准入中金额","准入通过笔数","准入通过金额","内审中笔数","内审中金额","内审完成笔数","内审完成金额","申报中笔数","申报中金额","未放款（储备）笔数","未放款（储备）金额","本月放款笔数","本月金额","本年放款笔数","本年放款金额"};
		PageResult<Object[]> pageResult = loanService.findLoanApplyStatis();
		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
		o2oPagingInfo.setFormats(new Format[]{null,new DecimalFormat(",##0"),new DecimalFormat(",##0"),new DecimalFormat(",##0"),new DecimalFormat(",##0"),new DecimalFormat(",##0"),new DecimalFormat(",##0"),new DecimalFormat(",##0"),new DecimalFormat(",##0"),new DecimalFormat(",##0"),new DecimalFormat(",##0"),new DecimalFormat(",##0"),new DecimalFormat(",##0"),new DecimalFormat(",##0"),new DecimalFormat(",##0"),new DecimalFormat(",##0"),new DecimalFormat(",##0"),new DecimalFormat(",##0"),new DecimalFormat(",##0")});
		o2oPagingInfo.setFirstRenderColumnIndex(0);
		ViewContext.put("paginationInfo", o2oPagingInfo);
		ViewContext.put("reporto2ourl", reporto2ourl);
		return "statistics/index";
 	}
	/**
	 * @Description:
	 *               <p>
	 *               loan-11722 增加业务汇总统计功能  导出
	 *               </p>
	 * @author CXW
	 * @date 2017年6月23日 下午1:57:29
	 * @version V1.0
	 */
	@RequestMapping("/download_statis")
	public void findLoanApplyStatisExport(HttpServletResponse httpServletResponse, HttpServletRequest request){
		PageResult<Object[]> pageResult = loanService.findLoanApplyStatiseExcel();
		String path = LoanapplyController.class.getResource("/excelTemplate").getPath();
		StringBuffer s = new StringBuffer();
		String outPath = s.append(path).append("/ywhz_data.xls").toString();
		OutputStream os = null;
		try {
			String outfile = "业务汇总统计.xls";
			String userAgent = request.getHeader("User-Agent");
			byte[] bytes = userAgent.contains("MSIE") ? outfile.getBytes() : outfile.getBytes("UTF-8");
			outfile = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
			File f = new File(outPath);
			if (!f.exists())
				throw new RuntimeIOException("file not found");
			os = httpServletResponse.getOutputStream();
			httpServletResponse.reset();
			httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
			httpServletResponse.setContentType("application/octet-stream; charset=utf-8");
			DefaultRowWriteHandler defaultRowWriteHandler = new DefaultRowWriteHandler(pageResult.getData());
			defaultRowWriteHandler.setRowIndex(3);
			defaultRowWriteHandler.setTotalCols(new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18});
			ExcelApiHelper.writeHandle(httpServletResponse.getOutputStream(), LoanapplyController.class.getResourceAsStream("/excelTemplate/ywhz_data.xls"), defaultRowWriteHandler);
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			IoUtil.fileToOutputStream(f, byteArrayOutputStream);
			os.write(byteArrayOutputStream.toByteArray());
			os.flush();
		} catch (IOException e) {
			throw new RuntimeIOException(e);
		} finally {
			IoUtil.close(os);
		}
	}
}
